package com.tangw.management.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tangw.management.model.User;
import com.tangw.management.service.UserService;

/**
 * 
 * @author TangW
 * @version 1.0
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	private static final Logger log = LoggerFactory.getLogger(UserController.class);

	@Resource(name = "userService")
	private UserService service;

	@RequestMapping(method = RequestMethod.GET)
	public String userHome() {
		log.info(" User page. ");
		return "user/userHome";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listUser() {
		log.info("/user/list");
		ModelAndView mv = new ModelAndView();
		List<User> userList = service.listUser(null);
		mv.addObject("userList", userList);
		mv.setViewName("user/listUser");
		return mv;
	}

	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public ModelAndView userCount() {
		log.info("/user/count");
		Long userCount = service.countUser();
		ModelAndView mv = new ModelAndView();
		mv.addObject("userCount", userCount);
		mv.setViewName("user/countUser");
		return mv;
	}

	@RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
	public ModelAndView userShow(@PathVariable("id") Integer id) {
		log.info("/user/show");
		User userShow = service.findUserById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("userShow", userShow);
		mv.setViewName("user/showUser");
		return mv;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String userAdd() {
		log.info("/user/add");
		return "user/addUser";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String doUserAdd(@ModelAttribute("user") User user) {
		log.info("/user/do add");
		user.setCreate_time(new Date());
		service.createUser(user);
		return "redirect:/user/list";
	}

	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.GET)
	public ModelAndView userEdit(@PathVariable("userId") Integer id) {
		log.info("/user/edit");
		User userEdit = service.findUserById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("userEdit", userEdit);
		mv.setViewName("user/editUser");
		return mv;
	}

	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.POST)
	public String doUserEdit(@ModelAttribute("userEdit") User user) {
		log.info("/user/do edit");
		service.updateUser(user);
		return "redirect:/user/list";
	}

	@RequestMapping(value = "/del/{userId}", method = RequestMethod.GET)
	public String del(@PathVariable("userId") Integer id) {
		log.info("/user/del");
		service.deleteById(id);
		return "redirect:/user/list";
	}
}
